package com.lzk.controller;

import com.lzk.domain.ResponseResult;
import com.lzk.domain.entity.Menu;
import com.lzk.domain.vo.MenuTreeVo;
import com.lzk.domain.vo.MenuVo;
import com.lzk.domain.vo.RoleMenuTreeSelectVo;
import com.lzk.service.MenuService;
import com.lzk.utils.BeanCopyUtils;
import com.lzk.utils.SystemConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.naming.spi.ResolveResult;
import java.util.List;

/**
 * author: 刘中魁
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    //获取菜单列表
    @GetMapping("/list")
    private ResponseResult getMenuList(Menu menu){
        List<Menu> menuList = menuService.getMenuList(menu);
        List<MenuVo> menuVos = BeanCopyUtils.copyBeanList(menuList, MenuVo.class);
        return ResponseResult.okResult(menuVos);
    }

    //添加菜单
    @PostMapping
    private ResponseResult addMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    //根据id查询菜单信息
    @GetMapping("/{id}")
    private ResponseResult selectMenuMes(@PathVariable long id){
        return menuService.selectMenuMes(id);
    }

    //更新菜单
    @PutMapping
    private ResponseResult updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    //删除菜单
    @DeleteMapping("/{menuid}")
    private ResponseResult delectMenu(@PathVariable long menuid){
        return menuService.delectMenu(menuid);
    }

    @GetMapping("/treeselect")
    public ResponseResult treeselect() {
        //复用之前的selectMenuList方法。方法需要参数，参数可以用来进行条件查询，而这个方法不需要条件，所以直接new Menu()传入
        List<Menu> menuList = menuService.getMenuList(new Menu());
        List<MenuTreeVo> options =  SystemConverter.buildMenuSelectTree(menuList);
        return ResponseResult.okResult(options);
    }

    //---------------------修改角色-根据角色id查询对应角色菜单列表树--------------------------

    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public ResponseResult roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        List<Menu> menus = menuService.getMenuList(new Menu());
        List<Long> checkedKeys = menuService.selectMenuListByRoleId(roleId);
        List<MenuTreeVo> menuTreeVos = SystemConverter.buildMenuSelectTree(menus);
        RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(checkedKeys,menuTreeVos);
        return ResponseResult.okResult(vo);
    }


}
